# Contributor: Shiz <hi@shiz.me>
# Maintainer: Shiz <hi@shiz.me>
pkgname=mailutils
pkgver=3.9
pkgrel=0
pkgdesc="GNU swiss army knife of electronic mail handling"
url="https://mailutils.org/"
arch="all !s390x"
license="GPL-3.0-or-later"
replaces="mailx"
depends_dev="$pkgname-libs=$pkgver-r$pkgrel"
makedepends="readline-dev libtool"
checkdepends="autoconf"
subpackages="$pkgname-dev $pkgname-doc $pkgname-libs $pkgname-servers $pkgname-mh"
source="https://ftp.gnu.org/gnu/mailutils/mailutils-$pkgver.tar.xz
	disable-koi8-r-test.patch"

build() {
	./configure \
		--build=$CBUILD \
		--host=$CHOST \
		--prefix=/usr
	make
}

check() {
	# Exclude imap4d tests as they require chdir("/root") which won't work.
	sed -i 's/$(IMAP4D_DIR)/# $(IMAP4D_DIR)/g' Makefile
	make check
}

package() {
	# re-enable imap4d subdirectory for install
	sed -i 's/# $(IMAP4D_DIR)/$(IMAP4D_DIR)/g' Makefile
	make DESTDIR="$pkgdir" install

	cd "$pkgdir"
	rm usr/lib/charset.alias
	# No need for these to be suid/sgid root.
	chmod u-s usr/sbin/mda
	chmod g-s usr/bin/dotlock
}

servers() {
	pkgdesc="$pkgdesc (servers)"

	mkdir -p "$subpkgdir"/usr/sbin
	local server; for server in pop3d imap4d comsatd; do
		mv "$pkgdir"/usr/sbin/$server "$subpkgdir"/usr/sbin/
	done
}

libs() {
	pkgdesc="$pkgdesc (libraries)"

	mkdir -p "$subpkgdir"/usr
	mv "$pkgdir"/usr/lib "$subpkgdir"/usr/
}

mh() {
	pkgdesc="$pkgdesc (MH compatibility)"

	mkdir -p "$subpkgdir"/usr/bin \
		"$subpkgdir"/usr/share/$pkgname

	mv "$pkgdir"/usr/bin/mu-mh "$subpkgdir"/usr/bin/
	mv "$pkgdir"/usr/share/$pkgname/mh "$subpkgdir"/usr/share/$pkgname/
	rmdir -p "$pkgdir"/usr/share/$pkgname || true
}

sha512sums="1eef9bebeeafbf974e54b87705444ee02b663a86e22442a9ec5db16ade38e6e9a3079ee8a8267393e97d6e9e477917617b2e1d57d3287ab19e4df73b66e5a8d4  mailutils-3.9.tar.xz
d0d78bba10d3ce039bb00657a570fb9411fabf448548994860285701939ae52afd15c007daa85bb18662a67153dda86069afe240b430fbe1b28a45755108f477  disable-koi8-r-test.patch"
